package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StateEnum;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.enums.WorkType;
import cn.cnf.common.exception.CustomException;
import cn.cnf.entity.Comic;
import cn.cnf.entity.Novel;
import cn.cnf.entity.User;
import cn.cnf.service.ChapterService;
import cn.cnf.service.ComicService;
import cn.cnf.service.NovelService;
import cn.cnf.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhuMeiYi
 * @date 2020/12/26 13:33
 * @description 不做登录限制的URL
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/cnf")
public class AnyRequestController {

    @Autowired
    private UserService userService;

    @Autowired
    private ComicService comicService;

    @Autowired
    private NovelService novelService;

    @Autowired
    private ChapterService chapterService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        try {
            return Result.success(userService.register(user));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/usernameExist")
    public Result usernameExist(@RequestParam("username") String username) {
        try {
            return Result.success(userService.usernameExist(username));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 全部漫画，分页
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/comicList")
    public Result getComic(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize,String name) {

        IPage<Comic> comicIPage = comicService.selectPageVo(current, pageSize,name);

        return Result.success(comicIPage);
    }

    /**
     * 搜索名字相似
     *
     * @param current
     * @param pageSize
     * @param comicName
     * @return
     */
    @GetMapping("/comicSearch")
    public Result searchComicByName(@RequestParam("current") Integer current,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("comicName") String comicName) {
        try {
            return Result.success(comicService.searchByName(current, pageSize, comicName));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/novelSearch")
    public Result searchNovelByName(@RequestParam("current") Integer current,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("novelName") String novelName) {
        try {
            return Result.success(novelService.searchByName(current, pageSize, novelName));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 全部章节
     *
     * @param fromId
     * @return
     */
    @GetMapping("/chapter")
    public Result getChapterById(String fromId, Integer workType) {
        try {
            return Result.success(chapterService.getChapterById(fromId, WorkType.getByValue(workType)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/recommendComic")
    public Result getRecommendComic(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(comicService.getAllRecommend(current, pageSize));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/recommendNovel")
    public Result getRecommendNovel(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(novelService.getAllRecommend(current, pageSize));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 最新更新
     *
     * @param current
     * @param pageSize
     * @param workType
     * @return
     */
    @GetMapping("/recentlyUpdate")
    public Result recentlyUpdated(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize, @RequestParam("workType") Integer workType) {
        WorkType type = WorkType.getByValue(workType);
        if (type == null) {
            return Result.warn("未知的作品类型");
        }
        try {
            if (type.equals(WorkType.COMIC)) {
                return Result.success(comicService.getRecentlyUpdate(current, pageSize));
            } else if (type.equals(WorkType.NOVEL)) {
                return Result.success(novelService.getRecentlyUpdate(current, pageSize));
            } else {
                return Result.warn("未知的作品类型");
            }
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/sendPasswordEmail")
    public Result sendPasswordEmail(@RequestParam("username") String username) {
        try {
            userService.sendPasswordEmail(username);
            return Result.success();
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/restPassword")
    public Result restPassword(@RequestParam("token") String token, @RequestParam("password") String password) {
        try {
            return Result.success(userService.resetPasswordByToken(token, password));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/novelList")
    public Result novelList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize,String name) {
        try {
            return Result.success(novelService.selectPage(current, pageSize,name));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 收藏榜
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/comicCollectionRank")
    public Result comicCollectionRank(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(comicService.rank(current, pageSize, Comic::getCollectNum));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 评论榜
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/comicCommentRank")
    public Result comicCommentRank(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(comicService.rank(current, pageSize, Comic::getCommentNum));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 订阅榜
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/comicSubscribeRank")
    public Result comicSubscribeRank(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(comicService.rank(current, pageSize, Comic::getSubscribeNum));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 收藏榜
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/novelCollectionRank")
    public Result novelCollectionRank(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(novelService.rank(current, pageSize, Novel::getCollectNum));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 评论榜
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/novelCommentRank")
    public Result novelCommentRank(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(novelService.rank(current, pageSize, Novel::getCommentNum));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 订阅榜
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/novelSubscribeRank")
    public Result novelSubscribeRank(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(novelService.rank(current, pageSize, Novel::getSubscribeNum));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/getByState")
    public Result getByState(@RequestParam("current") Integer current
            , @RequestParam("pageSize") Integer pageSize
            , @RequestParam("workType") Integer workType
            , @RequestParam("state") Integer state) {
        StateEnum stateEnum = StateEnum.getByValue(state);
        if (stateEnum == null) {
            return Result.warn("未知的作品状态");
        }
        try {
            WorkType type = WorkType.getByValue(workType);
            if (WorkType.COMIC.equals(type)) {
                return Result.success(comicService.getByState(current, pageSize, stateEnum));
            } else if (WorkType.NOVEL.equals(type)) {
                return Result.success(novelService.getByState(current, pageSize, stateEnum));
            } else {
                return Result.warn("未知的作品类型");
            }
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }


    }

}
